<template>
  <el-dialog title="新增部门" :visible="showDialog" @close="close">
    <el-form ref="addDept" :model="formData" :rules="rules" label-width="120x">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" placeholder="2-10字符" style="width: 80%" size="mini"/>
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width: 80%" placeholder="2-10字符" size="mini"/>
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select v-model="formData.managerId" style="width: 80%" placeholder="2-10字符" size="mini">
<!--          编写下来选项  循环负责人的数据  label表示显示的字段  value为存储字段-->
          <el-option v-for="item in managerList" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" type="textarea" :row="4" style="width: 80%"  placeholder="1-100字符" size="mini"/>
      </el-form-item>
      <el-form-item>
        <el-row type="flex" justify="center">
          <el-col :span="12" align="center">
            <el-button @click="btnOK" type="primary" size="mini">确认</el-button>
            <el-button @click="close" size="mini">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
  import {getDepartment,getManagerList,addDepartment} from '@/api/department'
  export default {
    props:{
      showDialog:{
        type: Boolean,
        default: false
      },
      currentNodeId:{
        type: Number,
        default: null
      }
    },
    data(){
      return{
        managerList: [],//存储负责人列表
        formData: {
          code: '',
          introduce: '',
          managerId: '',
          name: '',
          pid: '',
        },
        rules: {
          code: [
            { required: true, message: '部门编码不能为空', trigger: 'blur' },
            {min: 2, max: 10, message: '部门编码的长度为2-10个字符', trigger: 'blur'},
            {
              trigger: 'blur',
              // 自定义校验模式
              validator: async(rule, value, callback) => {
                // value就是输入的编码
                let result = await getDepartment()
                // 判断是否是编辑模式
                if (this.formData.id) {
                  // 编辑场景
                  result = result.filter(item => item.id !== this.formData.id)
                }
                // result数组中是否存在 value值
                //some判断数组是否存在某个值，里面是回调函数
                if (result.some(item => item.code === value)) {
                  callback(new Error('部门中已经有该编码了'))
                } else {
                  callback()
                }
              }
            }
            ],

          introduce: [
            { required: true, message: '部门介绍不能为空', trigger: 'blur' },
            {min: 1, max: 100, message: '部门介绍的长度为1-100个字符', trigger: 'blur'}
            ],

          managerId: [
            { required: true, message: '部门负责人不能为空', trigger: 'blur' }
            ],

          name: [
            { required: true, message: '部门名称不能为空', trigger: 'blur' },
            {min: 2, max: 10, message: '部门名称的长度为2-10个字符', trigger: 'blur'},
            {
              trigger: 'blur',
              // 自定义校验模式
              validator: async(rule, value, callback) => {
                // value就是输入的编码
                let result = await getDepartment()
                // 判断是否是编辑模式
                if (this.formData.id) {
                  // 编辑场景 排除自身
                  result = result.filter(item => item.id !== this.formData.id)
                }
                // result数组中是否存在 value值
                //some判断数组是否存在某个值，里面是回调函数
                if (result.some(item => item.name === value)) {
                  callback(new Error('部门中已经有该编码了'))
                } else {
                  callback()
                }
              }
            }
            ],
        }
      }
    },
    created() {
      this.getManagerList();
    },
    methods:{
      async getManagerList(){
        const result = await getManagerList();
        this.managerList = result;
      },
      close(){
        //重置表单
        this.$refs.addDept.resetFields()
        //修改父组件的值   子传父
        this.$emit('update:showDialog',false)//自定义事件
      }
    },
    btnOK(){
      //表单校验
      //获取表单实例
      this.$refs.addDept.validate(async isOK=>{
        if (isOK){
          await addDepartment({...this.formData,pid : this.currentNodeId})
          //通知父组件去更新
          this.$emit('updateDeptment')
          //提示实现
          this.$message.success('新增部门成功')
          this.close()
        }
      })
    },
  }
</script>
